import React, { Component } from 'react';
import { connect } from "react-redux";
import { messages } from "share/common";
import {Button, Form, message, Switch} from "antd";
import Priority from "components/template/priority";
import autoCheckService from "./auto-check-receipt.service"

const FormItem = Form.Item;

@Form.create()
@connect(mapStateToProps)
class AutoCheckReceiptDetail extends Component {

  state = {
    loading: false,
    enabled: this.props.record.receiptCheckConfigId ? this.props.record.enabled : true,
    priorityValue: this.props.record.receiptCheckConfigId ? {
      levelCode: this.props.record.levelCode,
      levelOrgId: this.props.record.levelOrgId,
      levelOrgName: this.props.record.levelOrgName,
      distribution: {
        companies: this.props.record.companies && this.props.record.companies.map((id, index) => {
          return { id, name: this.props.record.companyNameList[index] }
        })
      }
    } : {
      levelCode: this.props.authPolicyAll ? 'TENANT' : 'COMPANY',
      levelOrgId: this.props.authPolicyAll ? this.props.tenant.id : this.props.company.id,
      levelOrgName: this.props.authPolicyAll ? this.props.tenant.tenantName : this.props.company.name,
    },
  };

  handleSave = () => {
    const { record } = this.props;
    const { priorityValue } = this.state;
    this.props.form.validateFieldsAndScroll((err, values) => {
      if (!err) {
        if (!(priorityValue && priorityValue.levelCode)) {
          message.error(messages('expense-14.key366')/*请选择所属组织*/);
          return
        }
        let companies = priorityValue.distribution && priorityValue.distribution.companies;
        if (companies && companies.length === 0) {
          message.error(messages('expense-14.key495')/*请选择启用公司*/);
          return
        }
        let params = {
          levelCode: priorityValue.levelCode,
          levelOrgId: priorityValue.levelOrgId,
          enabled: values.enabled,
        };
        record.receiptCheckConfigId && (params.receiptCheckConfigId = record.receiptCheckConfigId);
        companies && companies.length && (params.companies = companies.map(item => item.id));
        this.setState({ loading: true });
        autoCheckService.saveConfig(params).then(() => {
          message.success(messages('common.save.success',{arg1: ''})/*{arg1} 保存成功*/);
          this.props.close(true);
        }).finally(() => {
          this.setState({ loading: false });
        })
      }
    })
  };

  render() {
    const { record, form: { getFieldDecorator } } = this.props;
    const { loading, enabled, priorityValue } = this.state;
    return (
      <div className="auto-check-receipt-detail">
        <Form className="new-form">
          <FormItem label={messages('common.column.status')/*状态*/}>
            {getFieldDecorator('enabled', {
              valuePropName: 'checked',
              initialValue: record.receiptCheckConfigId ? record.enabled : true
            })(
              <Switch onChange={enabled => this.setState({ enabled })}/>
            )}
            <span style={{ marginLeft: 10 }}>
              {enabled ? messages('common.status.enable')/*启用*/ : messages('common.status.disable')/*禁用*/}
            </span>
          </FormItem>
          <Priority
            top="TENANT"
            formItemLayout={{}}
            defaultValue={priorityValue}
            required
            disabledTop={!!record.receiptCheckConfigId}
            companyProps={{ disabled: !!record.receiptCheckConfigId }}
            onChange={value => this.setState({ priorityValue: value })}
          />
          <div className="slide-footer">
            <Button type="primary" loading={loading} onClick={this.handleSave}>{messages('common.save')/*保存*/}</Button>
            <Button onClick={() => this.props.close()}>{messages('common.cancel')/*取消*/}</Button>
          </div>
        </Form>
      </div>
    )
  }
}

function mapStateToProps(state) {
  return {
    authPolicyAll: state.main.authPolicyAll,
    company: state.login.company,
    tenant: state.login.tenant,
  }
}

export default AutoCheckReceiptDetail;
